﻿using Mforj.Common.Utils;
using smforj.common.dao;
using smforj.common.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace smforj.common.service
{
    public interface ICommodityService
    {
        /// <summary>
        /// 保存或更新商品类型信息
        /// </summary>
        /// <param name="list">商品类型信息列表</param>
        /// <returns></returns>
        int SaveOrUpdateCommodityType(List<CommodityType> list);

        /// <summary>
        /// 查询商品类型信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        List<CommodityType> GetCommodityTypeList(IQuery query);


        /// <summary>
        /// 根据编码查询商品信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>商品信息列表</returns>
        Commodity GetCommodityRemote(IQuery query);
    }


    public class CommodityServiceImpl : ICommodityService
    {

        private static HttpHelper httpHelper = new HttpHelper();
        private static JsonHelper jsonHelper = new JsonHelper();
        private ICommodityDao dao = new CommodityDaoImpl();

        public int SaveOrUpdateCommodityType(List<CommodityType> list)
        {
            return dao.SaveOrUpdateCommodityType(list);
        }

        public List<CommodityType> GetCommodityTypeList(IQuery query)
        {
            return dao.GetCommodityTypeList(query);
        }

        /// <summary>
        /// 查询商品信息，要通过接口
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <returns></returns>
        public model.Commodity GetCommodityRemote(model.IQuery query)
        {

            String url = query.GetUrl() + "?token=" + GlobalCache.loginUser.access_token + "&rfid=" + query.GetID();
            String response = httpHelper.GetRequestJson(url);

            CommodityResponse cres = jsonHelper.JsonDeSerializer<CommodityResponse>(response);
            if (cres == null)
                cres = new CommodityResponse() { isSuccess = false, message = "return data Exception" };

            if (cres.isSuccess)
                return (Commodity)cres.GetData();

            return null;
        }
    }
}
